<?php
/**
 * 
 */
namespace App\Http\Controllers;

use App\Http\Services\AjaxService;
use App\Http\Services\MCore;
use Illuminate\Http\Request;
class TestController extends Controller
{
	
	public function index(Request $request)
	{
		$code = MCore::get_var('code', $request, 0);
		$msg = 'SERVER_NAME: '.$_SERVER['SERVER_NAME'].', SERVER_ARRD: '.$_SERVER['SERVER_ADDR'].', REMOTE_ADDR: '.$_SERVER['REMOTE_ADDR'].', HTTP_CLIENT_IP: '.$this->get_ip();
		AjaxService::ajax_return(['msg' => 'SUCCESS'], ['code' => $code, 'msg' => $msg]);
	}

	public function get_client_ip()
	{
	    foreach (array(
	                'HTTP_CLIENT_IP',
	                'HTTP_X_FORWARDED_FOR',
	                'HTTP_X_FORWARDED',
	                'HTTP_X_CLUSTER_CLIENT_IP',
	                'HTTP_FORWARDED_FOR',
	                'HTTP_FORWARDED',
	                'REMOTE_ADDR') as $key) {
	        if (array_key_exists($key, $_SERVER)) {
	            foreach (explode(',', $_SERVER[$key]) as $ip) {
	                $ip = trim($ip);
	                //会过滤掉保留地址和私有地址段的IP，例如 127.0.0.1会被过滤
	                //也可以修改成正则验证IP
	                if ((bool) filter_var($ip, FILTER_VALIDATE_IP,
	                                FILTER_FLAG_IPV4 |
	                                FILTER_FLAG_NO_PRIV_RANGE |
	                                FILTER_FLAG_NO_RES_RANGE)) {
	                    return $ip;
	                }
	            }
	        }
	    }
	    return null;
	}

	//不同环境下获取真实的IP
	public function get_ip(){
	    //判断服务器是否允许$_SERVER
	    if(isset($_SERVER)){    
	        if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])){
	            $realip = $_SERVER['HTTP_X_FORWARDED_FOR'];
	        }elseif(isset($_SERVER['HTTP_CLIENT_IP'])) {
	            $realip = $_SERVER['HTTP_CLIENT_IP'];
	        }else{
	            $realip = $_SERVER['REMOTE_ADDR'];
	        }
	    }else{
	        //不允许就使用getenv获取  
	        if(getenv("HTTP_X_FORWARDED_FOR")){
	              $realip = getenv( "HTTP_X_FORWARDED_FOR");
	        }elseif(getenv("HTTP_CLIENT_IP")) {
	              $realip = getenv("HTTP_CLIENT_IP");
	        }else{
	              $realip = getenv("REMOTE_ADDR");
	        }
	    }

	    return $realip;
	}

	public function fapiao()
    {
        $url = 'http://webapi.xfyun.cn/v1/service/v1/ocr/invoice';
        $appId = '5c08c9d4';
        $appKey = '100861a960447bb47278d49411d33701';
        $timestamp = time();
        $param = [
            'engine_type' => 'invoice',
        ];
        $xParam = base64_encode(json_encode($param));
        $checkSum = md5($appKey.$timestamp.$xParam);

        $imagePath = 'D:\AppServ\www\super_framework\public\image\2.jpg';
//        var_dump($this->base64EncodeImage($imagePath));exit;
        $image = file_get_contents($imagePath);
        $requestParam = [
            'image' => base64_encode($image)
        ];


        $headers[] = 'X-CurTime:'.$timestamp;
        $headers[] = 'X-Param:'.$xParam;
        $headers[] = 'X-Appid:'.$appId;
        $headers[] = 'X-CheckSum:'.$checkSum;
        $headers[] = 'Content-Type:application/x-www-form-urlencoded; charset=utf-8';

        $result = $this->http_request($url, $requestParam, $headers);
        return $result;
    }


    /**
     * 发送post请求
     * @param string $url 请求地址
     * @param array $post_data post键值对数据
     * @return string
     */
    function http_request($url, $post_data, $headers) {
        $postdata = http_build_query($post_data);
        $options = array(
            'http' => array(
                'method' => 'POST',
                'header' => $headers,
                'content' => $postdata,
                'timeout' => 15 * 60 // 超时时间（单位:s）
            )
        );
        $context = stream_context_create($options);
        $result = file_get_contents($url, false, $context);

        return $result;
    }
}